var connection = require('./connection.js');
exports.getIndexData = function ( callback ) {
    var db = connection.conn();
    db.query('select * from job limit 16').on('end', function ( d ) {
        callback( d.result.rows );
    })
    // add error eventlistener then call callback method
    // the args '[]'
    // ok
    db.on('error', function ( e ) {
        callback([]);
        // please print e for develop
    });
    db.close(); 
};

// TODO
exports.getJobById = function ( jid , callback ) {
    // callback();
    var db = connection.conn();
    db.query('select * from job where jid='+jid).on('end',function(data){
        // console.log(d.result.rows,99);
        callback(data.result.rows);
    });
    db.on('error',function(e){
        callback([]);
    });
    db.close();
};

exports.getPagedData = function( pageId , callback ){
    var db = connection.conn();
    var page = parseInt(pageId)-1;
    db.query('select * from job LIMIT 20*'+page+', 20').on('end',function(data){
        console.log(data.result.rows);
        callback(data.result.rows);
    });
    db.on('error',function(e){
       callback([]);
    });
    db.close();
};
